#include <windows.h>
#include <stdio.h>
#define 	AVG_FILTER_PTS		(8)

typedef struct
{
	int x;
	int y;
}point;

HWND g_hwnd;
HDC g_hdc;

void draw_init()
{
	//获取console的设备上下文句柄
	g_hwnd = GetConsoleWindow();
	g_hdc = GetDC(g_hwnd);

	//调整一下console背景颜色，否则看不清线条
	system("color 3D");

	//起始位置，Windows中窗口的坐标系相当于直角坐标系第一象限翻转到第四象限
	MoveToEx(g_hdc, 0, 0, NULL);

}
void draw_point(point *pts, int len)
{
	MoveToEx(g_hdc, pts[0].x, pts[0].y, NULL);
	for (int i = 1; i < len; ++i)
	{
		LineTo(g_hdc, pts[i].x, pts[i].y);
	}
}

void filter(short *src, short*outData, int len)
{
	static short s_avg_filter[AVG_FILTER_PTS] = { 0 };
	static int s_avg_count = 0;
	int avg_sum = 0;
	for (int i = 0; i < len; i++)
	{
		avg_sum = 0;
		for (size_t k = AVG_FILTER_PTS - 1; k > 0; k--)
		{
			s_avg_filter[k] = s_avg_filter[k - 1];
			avg_sum += s_avg_filter[k];
		}
		s_avg_filter[0] = src[i];
		avg_sum += s_avg_filter[0];
		if (s_avg_count < AVG_FILTER_PTS)
		{
			s_avg_count++;
			outData[i] = s_avg_filter[0];
		}
		else
		{
			short ret = (short)(1.0*avg_sum / AVG_FILTER_PTS);
			outData[i] = ret;
		}
	}
}


int main(int argc, char const *argv[])
{
	draw_init();
	point pts[301] = {
		{1, 150},
		{2, 175},
		{3, 187},
		{4, 183},
		{5, 170},
		{6, 159},
		{7, 163},
		{8, 182},
		{9, 208},
		{10, 227},
		{11, 230},
		{12, 219},
		{13, 204},
		{14, 198},
		{15, 208},
		{16, 231},
		{17, 253},
		{18, 262},
		{19, 255},
		{20, 238},
		{21, 223},
		{22, 223},
		{23, 238},
		{24, 259},
		{25, 274},
		{26, 272},
		{27, 255},
		{28, 235},
		{29, 223},
		{30, 228},
		{31, 245},
		{32, 262},
		{33, 265},
		{34, 252},
		{35, 229},
		{36, 209},
		{37, 203},
		{38, 213},
		{39, 229},
		{40, 238},
		{41, 230},
		{42, 209},
		{43, 183},
		{44, 168},
		{45, 168},
		{46, 181},
		{47, 193},
		{48, 194},
		{49, 177},
		{50, 151},
		{51, 128},
		{52, 120},
		{53, 127},
		{54, 141},
		{55, 149},
		{56, 141},
		{57, 118},
		{58, 93},
		{59, 77},
		{60, 78},
		{61, 91},
		{62, 105},
		{63, 106},
		{64, 92},
		{65, 68},
		{66, 47},
		{67, 42},
		{68, 52},
		{69, 70},
		{70, 81},
		{71, 77},
		{72, 58},
		{73, 37},
		{74, 26},
		{75, 32},
		{76, 50},
		{77, 69},
		{78, 76},
		{79, 66},
		{80, 48},
		{81, 33},
		{82, 33},
		{83, 49},
		{84, 73},
		{85, 89},
		{86, 91},
		{87, 78},
		{88, 62},
		{89, 57},
		{90, 68},
		{91, 91},
		{92, 115},
		{93, 127},
		{94, 122},
		{95, 108},
		{96, 97},
		{97, 101},
		{98, 121},
		{99, 148},
		{100, 167},
		{101, 172},
		{102, 161},
		{103, 148},
		{104, 144},
		{105, 156},
		{106, 181},
		{107, 205},
		{108, 217},
		{109, 213},
		{110, 198},
		{111, 187},
		{112, 190},
		{113, 208},
		{114, 233},
		{115, 251},
		{116, 253},
		{117, 240},
		{118, 223},
		{119, 216},
		{120, 224},
		{121, 245},
		{122, 265},
		{123, 273},
		{124, 264},
		{125, 245},
		{126, 228},
		{127, 226},
		{128, 239},
		{129, 258},
		{130, 271},
		{131, 267},
		{132, 248},
		{133, 226},
		{134, 213},
		{135, 216},
		{136, 231},
		{137, 246},
		{138, 248},
		{139, 233},
		{140, 209},
		{141, 187},
		{142, 180},
		{143, 188},
		{144, 203},
		{145, 211},
		{146, 203},
		{147, 180},
		{148, 154},
		{149, 138},
		{150, 138},
		{151, 150},
		{152, 162},
		{153, 162},
		{154, 146},
		{155, 120},
		{156, 97},
		{157, 89},
		{158, 97},
		{159, 112},
		{160, 120},
		{161, 113},
		{162, 91},
		{163, 67},
		{164, 52},
		{165, 54},
		{166, 69},
		{167, 84},
		{168, 87},
		{169, 74},
		{170, 52},
		{171, 33},
		{172, 29},
		{173, 42},
		{174, 61},
		{175, 74},
		{176, 72},
		{177, 55},
		{178, 36},
		{179, 27},
		{180, 35},
		{181, 55},
		{182, 76},
		{183, 84},
		{184, 77},
		{185, 60},
		{186, 47},
		{187, 49},
		{188, 67},
		{189, 92},
		{190, 110},
		{191, 113},
		{192, 102},
		{193, 87},
		{194, 83},
		{195, 95},
		{196, 119},
		{197, 144},
		{198, 156},
		{199, 152},
		{200, 139},
		{201, 128},
		{202, 133},
		{203, 152},
		{204, 179},
		{205, 199},
		{206, 203},
		{207, 192},
		{208, 178},
		{209, 173},
		{210, 185},
		{211, 209},
		{212, 232},
		{213, 243},
		{214, 238},
		{215, 222},
		{216, 209},
		{217, 211},
		{218, 227},
		{219, 251},
		{220, 267},
		{221, 267},
		{222, 252},
		{223, 234},
		{224, 224},
		{225, 231},
		{226, 250},
		{227, 268},
		{228, 274},
		{229, 263},
		{230, 242},
		{231, 223},
		{232, 219},
		{233, 230},
		{234, 248},
		{235, 258},
		{236, 253},
		{237, 232},
		{238, 208},
		{239, 194},
		{240, 195},
		{241, 209},
		{242, 222},
		{243, 223},
		{244, 207},
		{245, 182},
		{246, 159},
		{247, 151},
		{248, 159},
		{249, 173},
		{250, 180},
		{251, 172},
		{252, 149},
		{253, 123},
		{254, 106},
		{255, 107},
		{256, 119},
		{257, 132},
		{258, 132},
		{259, 117},
		{260, 91},
		{261, 70},
		{262, 62},
		{263, 71},
		{264, 87},
		{265, 97},
		{266, 91},
		{267, 71},
		{268, 48},
		{269, 35},
		{270, 38},
		{271, 55},
		{272, 72},
		{273, 77},
		{274, 65},
		{275, 45},
		{276, 28},
		{277, 26},
		{278, 41},
		{279, 62},
		{280, 77},
		{281, 77},
		{282, 62},
		{283, 45},
		{284, 38},
		{285, 47},
		{286, 69},
		{287, 92},
		{288, 102},
		{289, 96},
		{290, 81},
		{291, 70},
		{292, 73},
		{293, 92},
		{294, 118},
		{295, 137},
		{296, 141},
		{297, 130},
		{298, 117},
		{299, 113},
		{300, 125},
		{301, 150},
	}
	;
	short  src[301] = { 0 };
	for (int i = 0; i < 301; ++i)
	{
		pts[i].x = 2 * i+100;
		src[i] = pts[i].y;
		pts[i].y += 200;
		
		//printf("%d, %d\n", pts[i].x, pts[i].y);
	}
	draw_point(pts, 301);

	short out[301] = { 0 };
	filter(src, out, 301);
	for (int i = 0; i < 301; ++i)
	{
		printf("%d ", out[i]);
		pts[i].y = out[i]+200;
	}
	draw_point(pts, 301);
	getchar();
	return 0;
}
